# Generated Cmake Pico project file
cmake_minimum_required(VERSION 3.13)

#设置为交叉编译 
set(CMAKE_SYSTEM_NAME "Generic")
 
 
#设置C标准
# set(CMAKE_C_STANDARD 11)
# set(CMAKE_CXX_STANDARD 17)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

#跳过检查 gcc是否可用
set(CMAKE_CXX_COMPILER_WORKS 1)
set(CMAKE_C_COMPILER_WORKS 1) 


#设置为debug编译
if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE "Debug")
endif()

 

# 设置GCC
set(CROSS_COMPILE "C:/Users/purple/.pico-sdk/toolchain/14_2_Rel1/bin/arm-none-eabi-")
set(CMAKE_C_COMPILER  ${CROSS_COMPILE}gcc.exe)
set(CMAKE_CXX_COMPILER ${CROSS_COMPILE}g++.exe)
set(CMAKE_ASM_COMPILER ${CROSS_COMPILE}gcc.exe)

message("gcc:" ${CMAKE_C_COMPILER})



# pico vscode 插件环境目录配置 
# Initialise pico_sdk from installed location
# (note this can come from environment, CMake cache etc)

# == DO NOT EDIT THE FOLLOWING LINES for the Raspberry Pi Pico VS Code Extension to work ==
if(WIN32)
    set(USERHOME $ENV{USERPROFILE})
else()
    set(USERHOME $ENV{HOME})
endif()
message("USERHOME:" ${USERHOME})

set(sdkVersion 2.1.1)
set(toolchainVersion 14_2_Rel1)
set(picotoolVersion 2.1.1)
set(picoVscode ${USERHOME}/.pico-sdk/cmake/pico-vscode.cmake)
if (EXISTS ${picoVscode})
    include(${picoVscode})
endif()
# ====================================================================================
set(PICO_BOARD pico CACHE STRING "Board type") 
# include(pico_sdk_import.cmake)

#设置工程名称
set(project_name_ rp2040_nano)
project(${project_name_} C ASM)
# pico_sdk_init()

#使能汇编.s
ENABLE_LANGUAGE(ASM)

#添加define
# add_definitions(-DUSE_HAL_DRIVER -DSTM32F103xB)

#设置 c ASM FLAGS
set(CMAKE_C_FLAGS "\
-mcpu=cortex-m0plus \
-mthumb \
-mfloat-abi=soft \
-Wall \
-fdata-sections \
-ffunction-sections")
message("CMAKE_C_FLAGS:" ${CMAKE_C_FLAGS})

set(CMAKE_ASM_FLAGS "${CMAKE_C_FLAGS} -x assembler-with-cpp -MMD -MP")


#设置 链接flags
set(CMAKE_C_LINK_FLAGS "-mcpu=cortex-m0plus \
-mcpu=cortex-m0plus -mthumb \
-Wl,--gc-sections \
-T${CMAKE_CURRENT_SOURCE_DIR}/link.ld \
-Wl,-Map=${project_name_}.map,--cref")
message("CMAKE_C_LINK_FLAGS:" ${CMAKE_C_LINK_FLAGS})

#debug配置
set(CMAKE_C_FLAGS_DEBUG "-O0 -g3")
set(CMAKE_CXX_FLAGS_DEBUG "-O0 -g3")

set (CMAKE_EXECUTABLE_SUFFIX ".elf")

# 目标文件
set(MY_TARGET ${project_name_})

#工程文件和include目录设置 
include_directories(inc .)
#添加.C和.S文件
add_executable(
  ${MY_TARGET}
  src/bs2_default_padded_checksummed.S src/gcc_startup.c src/gpio.c src/interrupt.c src/led.c src/main.c src/resets.c src/sysclk.c)


# 将elf文件转hex和bin
set(CMAKE_OBJCOPY "${CROSS_COMPILE}objcopy")
add_custom_command(TARGET ${MY_TARGET} POST_BUILD COMMAND ${CMAKE_OBJCOPY} -O ihex ${MY_TARGET}.elf ${project_name_}.hex)

# bin文件
add_custom_command(TARGET ${MY_TARGET} POST_BUILD COMMAND  ${CMAKE_OBJCOPY} -O binary -S ${MY_TARGET}.elf ${project_name_}.bin)

set(CMAKE_SIZE "${CROSS_COMPILE}size")
# 显示代码段大小
add_custom_command(TARGET ${MY_TARGET} POST_BUILD COMMAND ${CMAKE_SIZE} ${MY_TARGET}.elf)

# # 生产UF2文件
add_custom_command( TARGET ${MY_TARGET} POST_BUILD COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/../../tools/elf2uf2.exe  ${MY_TARGET}.elf ${MY_TARGET}.uf2)